package com.sphd.emanu.activity.send;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.adapter.SendAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.order.Order;
import com.sphd.emanu.utils.PermissionConfig;
import com.sphd.emanu.view.siderbar.SortModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * todo 派送模块
 *
 * @Author Hu
 * @Time 2017/8/7 -- 10:32.
 * @Email 87683202@qq.com
 */
public class SendActivity extends MVPBaseActivity<SendContract.View, SendPresenter> implements SendContract.View, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.find_all_orders)
    TextView findAllOrders;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    SendAdapter sendAdapter;
    Intent intent;
    Bundle bundle;
    User user;
    int state = 0;//0是正在派送,1是派送完成
    int cuntpage = 1;
    @BindView(R.id.change_state)
    LinearLayout changeState;
    List<Moudle> moudles;
    boolean isCanDo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(this);
        toolbar.setNavigationOnClickListener(v -> {
            if (state == 0) {
                finish();
            } else if (state == 1) {
                toolbar.setTitle("派送");
                changeState.setVisibility(View.VISIBLE);
                state = 0;
                cuntpage = 1;
                addData();
            }
        });
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                if (isCanDo) {
                    Order order = (Order) adapter.getItem(position);
                    new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE).setTitleText("确认该订单已送达了吗？")
                            .setConfirmText("确定").setConfirmClickListener(sweetAlertDialog1 -> {
                        showProgressDialog();
                        mPresenter.updateOrderStatus(order.getId(), 5, sweetAlertDialog1);

                    }).setCancelText("算了").show();
                } else {
                    toast(getString(R.string.no_perssion));
                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "派送");
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_SENDING_9);
        initAdapter();
        addData();
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        sendAdapter = new SendAdapter(R.layout.item_send_orders, new ArrayList<Order>(), context);
        sendAdapter.setEmptyView(emptyView);
        sendAdapter.openLoadAnimation();
        sendAdapter.setOnLoadMoreListener(this, myRecycler);
        myRecycler.setAdapter(sendAdapter);
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectOrderSend(user.getOid(), state, cuntpage);
    }

    @OnClick(R.id.find_all_orders)
    public void onViewClicked() {
        changeState.setVisibility(View.GONE);
        toolbar.setTitle("外卖订单");
        state = 1;
        cuntpage = 1;
        addData();
    }

    @Override
    public void sendList(List<Order> orderList) {
        sendAdapter.setState(state);
        dissProgressDialog();
        if (cuntpage == 1) {
            sendAdapter.setNewData(orderList);
        } else {
            sendAdapter.addData(orderList);
        }
        if (orderList.size() != 10) {
            sendAdapter.loadMoreEnd(false);
        } else {
            sendAdapter.loadMoreComplete();
        }
    }


    @Override
    public void onLoadMoreRequested() {
        cuntpage++;
        addData();
    }

    @Override
    public void recRes(int res, SweetAlertDialog sweetAlertDialog1) {
        dissProgressDialog();
        if (res == 1) {
            toast(getString(R.string.up_success));
            sweetAlertDialog1.dismiss();
            onRefresh();
        } else {
            toast(getString(R.string.up_failure));
        }
    }

    @Override
    public void onRefresh() {
        cuntpage = 1;
        addData();
    }
}
